/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica;

import fisica.CapaFisica;
import fisica.TriageFisica;
import hibernate.objeto.DiagnosticoYTriageInternacion;
import hibernate.objeto.Triage;

/**
 *
 * @author Gonzalo
 */
public class TriageLogica extends CapaLogica {

    /**
     * Crea y persiste el triage para el DiagnosticoYTriageInternacion del
     * paciente.
     *
     * @param datosDiagnosticoYTriageInternacion Los datos a cargar
     * correspondientes al triage
     * @param diagnosticoYTriageInternacion el diagnóstico de la internación a
     * la que se le asocia el triage
     * @return true si se agregó correctamente, false en caso contrario
     */
    public static boolean agregarTriage(Object[] datosDiagnosticoYTriageInternacion,
            DiagnosticoYTriageInternacion diagnosticoYTriageInternacion) {
        
        Triage triage = crearUObtenerObjetoTriage(
                datosDiagnosticoYTriageInternacion, diagnosticoYTriageInternacion);

        //Persiste el objeto
        if (CapaFisica.guardarOActualizarObjeto(triage)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Crea un objeto Triage con los datos pasados por parámetro.
     *
     * @param datosDiagnosticoYTriageInternacion Los datos a cargar
     * @param diagnosticoYTriageInternacion el diagnóstico de la internación a
     * la que se le asocia el triage
     * @return El objeto Triage creado
     */
    private static Triage crearUObtenerObjetoTriage(Object[] datosDiagnosticoYTriageInternacion,
            DiagnosticoYTriageInternacion diagnosticoYTriageInternacion) {
        
        Triage triage = diagnosticoYTriageInternacion.getTriage();
        //Si aun no tiene, se crea
        if (triage == null) {
            triage = new Triage();
        }

        triage.setTensionArterialSistolicaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TENSION_ARTERIAL_SISTOLICA]);
        
        triage.setTensionArterialDiastolicaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TENSION_ARTERIAL_DIASTOLICA]);

        triage.setFrecuenciaCardiacaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_FRECUENCIA_CARDIACA]);

        triage.setFrecuenciaRespiratoriaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_FRECUENCIA_RESPIRATORIA]);

        triage.setSaturacionOxigenoTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_SATURACION_OXIGENO]);

        triage.setTemperaturaTriage(
                (Float) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TEMPERATURA]);

        triage.setEvaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_EVA]);

        triage.setGlucemiaTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_GLUCEMIA]);

        triage.setTiempoFocoNeurologicoTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TIEMPO_FOCO_NEUROLOGICO]);

        triage.setTiempoDolorToracicoTriage(
                (Integer) datosDiagnosticoYTriageInternacion[
                TRIAGE_TIEMPO_DOLOR_TORACICO]);

        triage.setInconcienteTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_INCONCIENTE].toString());

        triage.setExcitadoTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_EXCITADO].toString());

        triage.setSangradoActivoTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_SANGRADO_ACTIVO].toString());

        triage.setTomaSintromTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_TOMA_SINTROM].toString());

        triage.setCrisisConvulsivaTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_CRISIS_CONVULSIVA].toString());

        triage.setTecTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_TEC].toString());

        triage.setCianosisTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_CIANOSIS].toString());

        triage.setRellenoCapilarTriage(
                datosDiagnosticoYTriageInternacion[
                TRIAGE_RELLENO_CAPILAR].toString());

        diagnosticoYTriageInternacion.setTriage(triage);

        return triage;
    }

    /**
     * Retorna el objeto Triage según el id.
     *
     * @param idTriage El id del triage
     * @return El objeto Triage consultado, null si no existe
     */
    public static Triage getTriagePorId(int idTriage) {
        return TriageFisica.getTriagePorId(idTriage);
    }

    /**
     * Chequea en el arreglo de los datos a cargar si los valores son distintos
     * de nulos. Si al menos uno es distinto, se debe crear el objeto
     *
     * @param datosDiagnosticoYTriageInternacion Los datos a cargar
     * correspondientes al triage
     * @return true si se debe crear, false en caso contrario
     */
    public static boolean seDebeCrearTriage(Object[] datosDiagnosticoYTriageInternacion) {
        if (((Float) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TEMPERATURA]) != 0 ) {
            return true;
        }
        int suma = (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TENSION_ARTERIAL_SISTOLICA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TENSION_ARTERIAL_DIASTOLICA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_FRECUENCIA_CARDIACA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_FRECUENCIA_RESPIRATORIA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_SATURACION_OXIGENO] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_EVA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_GLUCEMIA] +
                (Integer) datosDiagnosticoYTriageInternacion[
                SIGNOS_VITALES_TIEMPO_FOCO_NEUROLOGICO] +
                (Integer) datosDiagnosticoYTriageInternacion[
                TRIAGE_TIEMPO_DOLOR_TORACICO];

        int longitud = 0;
        for (int i = TRIAGE_INCONCIENTE; i <= TRIAGE_RELLENO_CAPILAR; i++) {
            longitud = longitud +
                    datosDiagnosticoYTriageInternacion[i].toString().length();
        }

        if ((suma + longitud) != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Retorna todo el objeto Triage en string.
     *
     * @param triage El objeto a escribir
     */
    public static String getTriageString(Triage triage) {
        String triageString =
                "idTriage: " +
                triage.getIdTriage() + "\n" +
                "tensionArterialSistolicaTriage: " +
                triage.getTensionArterialSistolicaTriage() + "\n" +
                "tensionArterialDiastolicaTriage: " +
                triage.getTensionArterialDiastolicaTriage() + "\n" +
                "frecuenciaCardiacaTriage: " +
                triage.getFrecuenciaCardiacaTriage() + "\n" +
                "frecuenciaRespiratoriaTriage: " +
                triage.getFrecuenciaRespiratoriaTriage() + "\n" +
                "saturacionOxigenoTriage: " +
                triage.getSaturacionOxigenoTriage() + "\n" +
                "temperaturaTriage: " +
                triage.getTemperaturaTriage() + "\n" +
                "glucemiaTriage: " +
                triage.getGlucemiaTriage() + "\n" +
                "tiempoFocoNeurologicoTriage: " +
                triage.getTiempoFocoNeurologicoTriage() + "\n" +
                "tiempoDolorToracicoTriage: " +
                triage.getTiempoDolorToracicoTriage() + "\n" +
                "inconcienteTriage: " +
                triage.getInconcienteTriage() + "\n" +
                "excitadoTriage: " +
                triage.getExcitadoTriage() + "\n" +
                "sangradoActivoTriage: " +
                triage.getSangradoActivoTriage() + "\n" +
                "tomaSintromTriage: " +
                triage.getTomaSintromTriage() + "\n" +
                "crisisConvulsivaTriage: " +
                triage.getCrisisConvulsivaTriage() + "\n" +
                "tecTriage: " +
                triage.getTecTriage() + "\n" +
                "cianosisTriage: " +
                triage.getCianosisTriage() + "\n" +
                "rellenoCapilarTriage: " +
                triage.getRellenoCapilarTriage() + "\n";

        return triageString;
    }

    /**
     * Retorna el texto HTML correspondiente a la descripcion del triage
     * de la internación.
     *
     * @param triage El triage correspondiente a la práctica ambulatoria
     * o de internación
     * @return El String que contiene el HTML
     */
    public static String getTriageHTML(
            Triage triage) {
        String triageHTML = "";

        if (triage != null) {
            triageHTML  = "<font size=\"3\" face=\"arial\" color=\"blue\">TRIAGE</font><br><br><hr /><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Tensión arterial sistólica: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTensionArterialSistolicaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Tensión arterial diastólica: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTensionArterialDiastolicaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Frecuencia cardíaca: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getFrecuenciaCardiacaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Frecuencia respiratoria: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getFrecuenciaRespiratoriaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Saturación oxígeno: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getSaturacionOxigenoTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Temperatura: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTemperaturaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Glucemia: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getGlucemiaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Tiempo foco neurológico: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTiempoFocoNeurologicoTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Tiempo de dolor toráxico: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTiempoDolorToracicoTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Inconciente: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getInconcienteTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Exitado: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getExcitadoTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Sangrado activo: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getSangradoActivoTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Toma sintrom: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTomaSintromTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Crisis convulsiva: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getCrisisConvulsivaTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Tec: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getTecTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Cianosis: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getCianosisTriage() + "</font><br><br>"
                    + "<font size=\"3\" face=\"arial\" color=\"blue\">Relleno capilar: </font><br>"
                    + "<font size=\"3\" face=\"arial\">" +
                triage.getRellenoCapilarTriage() + "</font><br><br>";
        } else {
            triageHTML = "<font size=\"3\" face=\"arial\" color=\"blue\">TRIAGE</font><br><br> " +
                    "<hr /><font size=\"3\" face=\"arial\">No posee</font><br></font><hr />";
        }
        return triageHTML;
    }
    
}
